//
//  GRAllBillViewController.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/27.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRAllBillViewController.h"
#import "GRAllBillTableViewCell.h"
#import "GRAllBillAction.h"
#import "GRAllBillModel.h"
//#import "GRInTheInstallmentViewController.h"
#import "GRMonthBillViewController.h"
#import "GRRepaymentViewController.h"
@interface GRAllBillViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger lastYMin;
}
@property (weak, nonatomic) IBOutlet UITableView *billList;
@property (strong, nonatomic) IBOutlet UIView *headerView;
@property(nonatomic,strong)NSMutableArray * billArr;
@property(nonatomic,strong)GRAllBillAction * billAction;
@property (weak, nonatomic) IBOutlet UILabel *totalPrice;

@property (strong, nonatomic) IBOutlet UIView *footView;
@property (weak, nonatomic) IBOutlet UIButton *lastYearLabel;

@property (weak, nonatomic) IBOutlet UILabel *curentYearLabel;

@property (weak, nonatomic) IBOutlet UIButton *returnPayBt;
@property (weak, nonatomic) IBOutlet UIButton *nextYearBt;
@property (weak, nonatomic) IBOutlet UIImageView *nextArrow;
@property (weak, nonatomic) IBOutlet UIImageView *lastArrow;


@end

@implementation GRAllBillViewController
-(NSMutableArray *)billArr{
    if (!_billArr) {
        _billArr = [NSMutableArray array];
    }
    return _billArr;
}

-(GRAllBillAction *)billAction{
    if (!_billAction) {
        _billAction = [[GRAllBillAction alloc]init];
    }
    return _billAction;
}

-(NSInteger)getCurrentYear{
    NSDate * date =  [NSDate date];
    NSCalendar * cal = [NSCalendar currentCalendar];
    NSDateComponents * d = [cal components:NSCalendarUnitYear fromDate:date];
    return [d year];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"全部账单";
    [self.billList registerNib:[UINib nibWithNibName:@"GRAllBillTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"billCell"];
    self.billList.tableHeaderView = self.headerView;
    self.billList.tableFooterView = self.footView;
    if (!_isPast) {
       self.currentYear = [self getCurrentYear];
    }
    [self loadData];
}
-(void)updateYear{
    self.curentYearLabel.text = [NSString stringWithFormat:@"%ld年",self.currentYear];
    if (lastYMin == 0 ||(lastYMin >self.currentYear-1)) {
        self.lastYearLabel.hidden = YES;
        self.lastArrow.hidden = YES;
    }else{
    self.lastYearLabel.hidden = NO;
    self.lastArrow.hidden = NO;
    [self.lastYearLabel setTitle:[NSString stringWithFormat:@"%ld年",self.currentYear -1] forState:UIControlStateNormal];
    }
        if (self.currentYear + 1 >[self getCurrentYear]) {
            self.nextYearBt.hidden = YES;
            self.nextArrow.hidden = YES;
        }else{
            self.nextYearBt.hidden = NO;
            self.nextArrow.hidden = NO;
            [self.nextYearBt setTitle:[NSString stringWithFormat:@"%ld年",self.currentYear +1] forState:UIControlStateNormal];
        }
    
}

-(void)loadData{
    [self.billAction getYearBill:self.currentYear CompleteBlock:^(BOOL success, id responseObject, GRError *error) {
        NSLog(@"%@",responseObject);
        if (success) {
            if(![responseObject[@"total"]  isKindOfClass:[NSNull class]]){
            self.totalPrice.text = [NSString stringWithFormat:@"%.2lf",[responseObject[@"total"] floatValue]] ;
                self.returnPayBt.backgroundColor =  [responseObject[@"total"] floatValue] >0 ?HEXCOLOR(0xFBD62D):HEXCOLOR(0xEEEEEE);
                [self.returnPayBt setTitleColor:[responseObject[@"total"] floatValue] >0 ? HEXCOLOR(0x222222):HEXCOLOR(0x999999) forState:UIControlStateNormal];
                self.returnPayBt.userInteractionEnabled = [responseObject[@"total"] floatValue] >0 ?YES:NO;
            }else{
                self.returnPayBt.userInteractionEnabled = NO;
                self.totalPrice.text = @"0.0";
                self.returnPayBt.backgroundColor = HEXCOLOR(0xEEEEEE);
                [self.returnPayBt setTitleColor:HEXCOLOR(0x999999) forState:UIControlStateNormal];
            }
            lastYMin = [responseObject[@"firstYear"] integerValue];
            [self updateYear];
            NSArray * billArr = responseObject[@"bills"];
            if (Array(billArr)) {
              [self.billArr removeAllObjects];
                for (NSDictionary * billDic in billArr) {
                    GRAllBillModel * billModel = [GRAllBillModel yy_modelWithDictionary:billDic];
                    [self.billArr addObject:billModel];
                }
                [self.billList  reloadData];
            }
        }
    }];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GRAllBillTableViewCell * billCell = [tableView dequeueReusableCellWithIdentifier:@"billCell" forIndexPath:indexPath];
    if (self.billArr.count >0) {
        billCell.billModel = self.billArr[indexPath.row];
        billCell.blankSpceView.hidden = YES;
    }else{
       billCell.blankSpceView.hidden = NO;
    }
    if (indexPath.row == 0) {
        billCell.monthLabel.backgroundColor = HEXCOLOR(0xFBD62D);
    }else{
        billCell.monthLabel.backgroundColor = HEXCOLOR(0xffffff);
    }

    return billCell;

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.billArr.count>0?self.billArr.count:1 ;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return self.billArr.count>0?45:272;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.billArr.count<=0)  return;
    GRMonthBillViewController * monthBillVC = [[GRMonthBillViewController alloc]init];
    GRAllBillModel * billModel = self.billArr[indexPath.row];
    monthBillVC.month = billModel.month;
    WEAKSELF
    monthBillVC.returnPaySuccess = ^{
        [weakSelf loadData];
        if (weakSelf.successReturnBlock != nil) {
            weakSelf.successReturnBlock();
        }
    };
    [self.navigationController pushViewController:monthBillVC animated:YES];
}

- (IBAction)skipInstallmentVCAction:(id)sender {
//    GRInTheInstallmentViewController * recordVC = [[GRInTheInstallmentViewController alloc] init];
//    [self.navigationController pushViewController:recordVC animated:YES];
}

- (IBAction)refundAction:(id)sender {
    GRRepaymentViewController * refundVC = [[GRRepaymentViewController  alloc]init];
    refundVC.refunType = RefundTypeAll;
    
    refundVC.timeStr = [NSString stringWithFormat:@"%ld年",self.currentYear];
    refundVC.price = [self.totalPrice.text floatValue];
    refundVC.successBlock = ^{
        [self loadData];
    };
    [self.navigationController pushViewController:refundVC animated:YES];
}

- (IBAction)lastYearAction:(id)sender {
    self.currentYear --;
    [self loadData];
}

- (IBAction)nextYearAction:(id)sender {
    self.currentYear ++;
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
